import {
  getBaseURL,
  requestUtil
} from "../../utils/requestUtil"
Page({
  data: {
    baseURL:'',
    username: '',
    password: '',
    nickname:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const baseURL = getBaseURL()
      this.setData({
        baseURL
      })

  },
  // 获取用户名
  nameChange: function (event) {
    const name = event.detail || event;
    this.setData({
      username: name,
    });
  },
  // 获取密码
  pwdChange: function (event) {
    const pwd = event.detail || event;
    this.setData({
      password: pwd,
    });
  },
  nicknameChange: function (event) {
    const nickname = event.detail || event;
    this.setData({
      nickname: nickname,
    });
  },
  register() {
    let username = this.data.username;
    let password = this.data.password;
    let nickname = this.data.nickname;
    console.log(`${username},${password},${nickname}`);
    if (username.length < 1 && password.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '请输入用户名密码',
      })
      return
    } else console.log(`${username},${password}`);
    if (username.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '用户名不能为空',
      })
      return
    } else if (password.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '用户名不能为空',
      })
      return
    }
    requestUtil({
      url: '/user/register',
      method: 'POST',
      data:{
        username,
        password,
        nickname
      }
    }).then(res => {
      console.log(res);
      if(res.code==505){
        wx.showToast({
          icon: 'none',
          title: '账号已存在',
        })
      }else if(res.code==200){
        wx.showToast({
          icon: 'none',
          title: '注册成功',
        })
        wx.redirectTo({
          url: '../login/login',
        })
      }
      this.setData({
        
      })
    })
  }
})